package pl.edu.pw.tm.zestawfiltrow.algorytmy;


public abstract class LinearFilter extends Filter {
	protected abstract int[][] getMask();
	
	protected int getMaskedValue(int x, int y, int[][] image){
		int r = 0, g = 0, b = 0;
		int[][] mask = getMask();
		int size = mask.length;
		int maskSum = 0;
		int offset = -(size-1)/2;
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				maskSum += mask[i][j];
			}
		}
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				r += getRed(image[x+i+offset][y+j+offset]) * mask[i][j];
				g += getGreen(image[x+i+offset][y+j+offset]) * mask[i][j];
				b += getBlue(image[x+i+offset][y+j+offset]) * mask[i][j];
			}
		}
		if(maskSum != 0){
			r = r/maskSum;
			g = g/maskSum;
			b = b/maskSum;
		}
		return toRGB(r, g, b);
	}
}
